﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using System.Collections.ObjectModel;
using WPF_Databases.Commands;
using Microsoft.Win32;
using System.IO;
using System.Data.Entity;
using WPF_Databases.ViewModels;


namespace WPF_Databases.ViewModels
{
    public class ViewModelMain : ViewModelBase
    {
        WorldLocationsEntities dbContext;
        private ObservableCollection<Continent> continents;
        private ObservableCollection<Country> countries;
        private ObservableCollection<City> cities;

        public RelayCommands SaveCommand
        {
            get
            {
                return new RelayCommands((p) => dbContext.SaveChanges());
            }
        }

        public RelayCommands ChangePictureCommand
        {
            get
            {
                return new RelayCommands(LoadPicture);
            }
        }

        private void LoadPicture(object obj)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "Image files|*.png;*.jpeg;*.jpg;*.bmp;";
            bool? result = dialog.ShowDialog();
            Country countryToModify = (Country)obj;

            if (result == true)
            {
                string fileName = dialog.FileName;

                FileInfo fileInfo = new FileInfo(fileName);
                if (fileName.Length <= 1024 * 1024 * 10)
                {
                    FileStream fs = new FileStream(fileName, FileMode.Open);
                    using (fs)
                    {
                        byte[] arr = new byte[fs.Length];
                        fs.Read(arr,0,(int)fs.Length);
                        countryToModify.Flag = arr;
                    }
                }
            }

        }

        public ViewModelMain()
        {
            dbContext = new WorldLocationsEntities();
            continents = new ObservableCollection<Continent>(dbContext.Continents);
            countries = new ObservableCollection<Country>(dbContext.Countries);
            cities = new ObservableCollection<City>(dbContext.Cities);
        }

        public ObservableCollection<Continent> Continnents
        {
            get
            {
                return continents;
            }
            set
            {
                continents = value;
            }
        }

        public ObservableCollection<Country> Countries
        {
            get
            {
                return countries;
            }
            set
            {
                countries = value;
            }
        }
    }
}
